package com.weike.wk_android.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.TextView;

import com.trello.rxlifecycle3.components.RxActivity;
import com.weike.wk_android.R;
import com.weike.wk_android.contract.RegisterContract;
import com.weike.wk_android.impl.ETPhoneListener;
import com.weike.wk_android.model.UserInfo;
import com.weike.wk_android.presenter.RegisterPresenter;
import com.weike.wk_android.utils.ActivityStackManager;
import com.weike.wk_android.utils.Utils;
import com.weike.wk_android.widget.toast.MyToast;

import androidx.annotation.Nullable;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class RegisterActivity extends RxActivity implements RegisterContract.View {
    @BindView(R.id.phone)
    EditText phone;
    @BindView(R.id.register_code)
    EditText registerCode;
    @BindView(R.id.register_invite_code)
    EditText registerInviteCode;
    @BindView(R.id.register_password)
    EditText registerPassword;
    @BindView(R.id.confirm_password)
    EditText confirmPassword;
    @BindView(R.id.register_check)
    CheckBox registerCheck;

    private Intent intent;
    // 判断是否选择同意协议
    private boolean isCheck;
    private RegisterContract.Presenter presenter;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        ButterKnife.bind(this);

        new RegisterPresenter(this, this);

        initListener();
    }

    /**
     * 手机号绑定事件
     */
    private void initListener() {
        phone.addTextChangedListener(new ETPhoneListener(phone));
    }

    @OnClick({R.id.register_close, R.id.go_login,
            R.id.get_code, R.id.register_check,
            R.id.submit, R.id.register_prot})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.register_close:
                close();
                break;
            case R.id.go_login:
                gotoLogin();
                break;
            case R.id.get_code:
                getCode();
                break;
            case R.id.register_check:
                checkProt();
                break;
            case R.id.submit:
                submit();
                break;
            case R.id.register_prot:
                gotoProt();
                break;
        }
    }

    /**
     * 跳转协议页面
     */
    private void gotoProt() {
    }

    /**
     * 提交信息
     */
    private void submit() {
        String username = phone.getText().toString();
        String code = registerCode.getText().toString();
        String inviteCode = registerInviteCode.getText().toString();
        String password = registerPassword.getText().toString();
        String confirm_password = confirmPassword.getText().toString();
        boolean isCheck = registerCheck.isChecked();

        if (TextUtils.isEmpty(username)) {
            MyToast.showToast(this, Utils.getString(R.string.phone_null));
        } else if (TextUtils.isEmpty(code)) {
            MyToast.showToast(this, Utils.getString(R.string.code_null));
        } else if (!TextUtils.equals(password, confirm_password)) {
            MyToast.showToast(this, Utils.getString(R.string.compare_pass));
        } else if (Utils.regPatternsPassword(this, password)) {
            return;
        } else if (!isCheck) {
            MyToast.showToast(this, Utils.getString(R.string.isCheck));
        }

//        presenter.register(new Register());
    }

    /**
     * 协议
     */
    private void checkProt() {
        if (isCheck) {
            isCheck = false;
        } else {
            isCheck = true;
        }
    }

    /**
     * 获取验证码
     */
    private void getCode() {
    }

    /**
     * 关闭页面
     */
    private void close() {
        finish();
    }

    /**
     * 跳转登录页面
     */
    private void gotoLogin() {
        intent = new Intent(this, LoginActivity.class);
        startActivity(intent);
        finish();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();

        clearUp();
    }

    private void clearUp() {
        intent = null;
    }

    @Override
    public void getRegister(UserInfo userInfo) {

    }

    @Override
    public void setPresenter(RegisterContract.Presenter presenter) {
        this.presenter = presenter;
    }
}
